﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CollegeWebsite.Areas.Administrator.Models;

namespace CollegeWebsite.Areas.Administrator.Controllers
{
    public class SubjectController : Controller
    {
        readonly CollegeWebsiteDataContext _dataContext = new CollegeWebsiteDataContext();
        //
        // GET: /Administrator/Subject/

        public ActionResult Index()
        {
            List<Subject> listSubject = _dataContext.Subjects.Select(s => s).ToList(); 
            return View(listSubject);
        }

        //
        // GET: /Administrator/Subject/Details/5

        public ActionResult Details(int id)
        {
            Subject subject = _dataContext.Subjects.Select(s => s).Single(s => (s.SubjectID == id));
            // Department dep__2 = _dataContext.Departments.Select(d => d).Where(d => (d.DepartmentID == id)).Single();
            if (subject != null)
            {
                return View(subject);
            }
            else
            {
                return RedirectToAction("Index");
            }

        }

        //
        // GET: /Administrator/Subject/Create

        public ActionResult Create()
        {
            Subject subject = new Subject();

            subject.Status = true;

            return View(subject);
        } 

        //
        // POST: /Administrator/Subject/Create

        [HttpPost]
        public ActionResult Create(Subject subject)
        {
            try
            {
                // TODO: Add insert logic here
                _dataContext.Subjects.InsertOnSubmit(subject);
                _dataContext.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Administrator/Subject/Edit/5
 
        public ActionResult Edit(int id)
        {
            Subject subject = _dataContext.Subjects.Single(s => s.SubjectID == id);
            return View(subject);
        }

        //
        // POST: /Administrator/Subject/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Subject newSubject)
        {
            try
            {
                // TODO: Add update logic here
                Subject oldSubject = _dataContext.Subjects.Single(s => s.SubjectID == id);

                oldSubject.SubjectName = newSubject.SubjectName;
                oldSubject.SujectDescription = newSubject.SujectDescription;
                oldSubject.Status = newSubject.Status;                
                
                _dataContext.SubmitChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Administrator/Subject/Delete/5
 
        public ActionResult Delete(int id)
        {
            Subject subject = _dataContext.Subjects.Single(s => s.SubjectID == id);
            return View(subject);
        }

        //
        // POST: /Administrator/Subject/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                Subject subject = _dataContext.Subjects.Single(s => s.SubjectID == id);
                _dataContext.Subjects.DeleteOnSubmit(subject);
                _dataContext.SubmitChanges();
 
                return RedirectToAction("Index");
            }
            catch(Exception ex)
            {
                ViewData["ErrorInfo"] = ex.Message;
                return View("Error");
            }
        }
    }
}
